home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / tutor / pro18 / keyboard.bas < prev    next >
Encoding:
BASIC Source File  |  1991-10-14  |  2.5 KB  |  83 lines

  1. 10 'KEYBOARD.BAS - From the GWBASIC Tutorial Series, #11
  2. 20 'October, 1991
  3. 30 '
  4. 40 'To illustrate key trapping, let's use a simple little program that just
  5. 50 'prints a message on the screen, changing the color every once in a while,
  6. 60 'then when one of the 'trapped' keys is pressed beeps and prints a message
  7. 70 'at the bottom of the screen.
  8. 80 '
  9. 90 'First, we define the 'user' keys:
  10. 100 '
  11. 110 KEY 16, CHR$(&H8) + CHR$(&H1E)
  12. 120 KEY 17, CHR$(&H8) + CHR$(&H20)
  13. 130 KEY 18, CHR$(&H8) + CHR$(&H21)
  14. 140 KEY 19, CHR$(&H8) + CHR$(&H19)
  15. 150 KEY 20, CHR$(&H0) + CHR$(&H1)
  16. 160 '
  17. 170 'Next, we need to turn on key trapping...
  18. 180 '
  19. 190 KEY(1) ON
  20. 200 KEY(13) ON
  21. 210 KEY(12) ON
  22. 230 KEY(16) ON
  23. 240 KEY(17) ON
  24. 250 KEY(18) ON
  25. 260 KEY(19) ON
  26. 270 KEY(20) ON
  27. 280 '
  28. 290 'Finally, tell the program what to do with these keys...
  29. 300 '
  30. 310 ON KEY(1) GOSUB 1000
  31. 320 ON KEY(13) GOSUB 2000
  32. 330 ON KEY(12) GOSUB 3000
  33. 350 ON KEY(16) GOSUB 5000
  34. 360 ON KEY(17) GOSUB 6000
  35. 370 ON KEY(18) GOSUB 7000
  36. 380 ON KEY(19) GOSUB 8000
  37. 390 ON KEY(20) GOSUB 9000
  38. 400 '
  39. 410 'Here's where your main program would go.  Instead, we'll have some fun!
  40. 420 '
  41. 430 CLS:KEY OFF
  42. 440 RANDOMIZE (-TIMER)
  43. 450 TIME = RND(500)
  44. 460 LOCATE 15,1,0
  45. 470 COL = COL + 1
  46. 480 IF COL > 16 THEN COL = 1
  47. 485 COLOR COL,0,0
  48. 490 PRINT "I'm waiting for you to press a key..."
  49. 500 FOR N = 1 TO TIME: NEXT N
  50. 510 GOTO 450
  51. 1000 ' F1 key pressed - print a message
  52. 1010 LOCATE 20,1,0
  53. 1020 PRINT "The last key you pressed was the F1 key...                "
  54. 1030 RETURN
  55. 2000 ' Right arrow pressed - print a message
  56. 2010 LOCATE 20,1,0
  57. 2020 PRINT "The last key you pressed was the right arrow key...       "
  58. 2030 RETURN
  59. 3000 ' Left arrow pressed - print a message
  60. 3010 LOCATE 20,1,0
  61. 3020 PRINT "The last key you pressed was the left arrow key...        "
  62. 3030 RETURN
  63. 5000 ' Alt-A key pressed - print a message
  64. 5010 LOCATE 20,1,0
  65. 5020 PRINT "The last key you pressed was the Alt-A key...             "
  66. 5030 RETURN
  67. 6000 ' Alt-D key pressed - print a message
  68. 6010 LOCATE 20,1,0
  69. 6020 PRINT "The last key you pressed was the Alt-D key...             "
  70. 6030 RETURN
  71. 7000 ' Alt-F key pressed - print a message
  72. 7010 LOCATE 20,1,0
  73. 7020 PRINT "The last key you pressed was the Alt-F key...             "
  74. 7030 RETURN
  75. 8000 ' Alt-P  key pressed - print a message
  76. 8010 LOCATE 20,1,0
  77. 8020 PRINT "The last key you pressed was the Alt-P key...             "
  78. 8030 RETURN
  79. 9000 ' Escape key pressed - print a message
  80. 9010 LOCATE 20,1,0
  81. 9020 PRINT "The last key you pressed was the ESCAPE key...            "
  82. 9030 RETURN
  83.